from common.ds_process_test_cases import ProcessJson
from jsonpath import jsonpath



class CheckExpected:

    def __init__(self):
        self.process_json = ProcessJson()
    def testreponse(self):
        import requests

        url = "https://atc.dsers.com/api/v1/users/info"

        headers = {
            'authorization': "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiYnFhNDk4NjBlNHJsbG1wdG9yM2ciLCJjdXJyZW50X3VzZXJfaWQiOiJicWE0OTg2MGU0cmxsbXB0b3IzZyIsInVzZXJfbmFtZSI6IiIsImVtYWlsIjoidGVzdG5ldzJAZHNlcnMuY29tIiwidXNlcl9sZXZlbCI6MSwic3RvcmVzIjpbImJtcjlwcWgwamQzZzAwZXV2YTMwIiwiYzlzNzRwcjR0OGswMDBidGMyNzAiLCJjOXYzbjBjcWJjYzAwMDhwbG1hMCIsImNhN2RxYjFtdHRqMDAwZGZrcWswIiwiY2FhNWxxcWZnNDgwMDA4a3I0cTAiXSwid29vX3N0b3JlcyI6bnVsbCwiY3JlYXRlZF9hdCI6MTY1NDQ5NTA4MywiZXhwIjoxNjU0NTAwOTYxfQ.6IZnh8ub9wFZRNguXoUL5lKyZFnqX5y7VLcgcs6j5o0",
            'User-Agent': "PostmanRuntime/7.19.0",
            'Accept': "*/*",
            'Cache-Control': "no-cache",
            'Postman-Token': "906dc07c-5692-48c6-95f0-4a3dea21c399,1d05a6c5-4814-47d1-bc77-af7edfef3dd7",
            'Host': "atc.dsers.com",
            'Accept-Encoding': "gzip, deflate",
            'Connection': "keep-alive",
            'cache-control': "no-cache"
        }

        response = requests.request("GET", url, headers=headers)
        return response

    # 根据预期结果的key 正则匹配接口响应的字段，然后根据获取的字段与预期结果进行对比
    def check_result(self, response, num):
        response_json = response.json()
        expected_json = self.process_json.get_expected()[num]
        expected_keys = list(expected_json.keys())
        for expected_key in expected_keys:
            # 获取接口返回的字段的值
            response_result = jsonpath(response_json, '$..{}'.format(expected_key))  # 接口返回的字段
            expected_result = expected_json.get(expected_key)
            # 字段的预期结果
            if response_result != False:
                if str(response_result[0]) == expected_result:
                    print('Succeed：' + expected_key)
                else:
                    print('The interface response did not find the relevant assertion section：' + expected_key)
            else:
                print('The interface response is inconsistent with the result of the related assertion field：', expected_key)


if __name__ == '__main__':
    check_expected = CheckExpected()
    check_expected.check_result(check_expected.testreponse(),num=0)

